Page({
  data: {
      category: [
          // {name:'果味',id:'guowei'},
          // {name:'蔬菜',id:'shucai'},
          // {name:'炒货',id:'chaohuo'},
          // {name:'点心',id:'dianxin'},
          // {name:'粗茶',id:'cucha'},
          // {name:'淡饭',id:'danfan'}
      ],
      detail:[],
      curIndex: 0,
      isScroll: false,
      toView: 'yinpin',
      products:[],
      meishi:[],
      yinpin:[],
      yiyao:[],
      shengxian:[],
      m:"meishi",
      y:"yinpin",
      yi:"yiyao",
      s:"shengxian",
  },
  onReady(){
      var self = this;
      wx.request({
          url:'http://www.gdfengshuo.com/api/wx/cate-detail.txt',
          success(res){
              console.log(res.data)
              self.setData({
                  detail : res.data
              })
          }
      });
      
  },
  switchTab(e){
   //使用缓存
    
    const self = this;
    this.setData({
      isScroll: true
    })
    setTimeout(function(){
      console.log(e.target.dataset.id)
      self.setData({
        toView: e.target.dataset.id,
        curIndex: e.target.dataset.index
      })
    },0)
    setTimeout(function () {
      self.setData({
        isScroll: false
      })
    },1)
      
  },
  toDetail(res){
    console.log(res)
    wx.setStorageSync('detail', res.currentTarget.dataset)
  },

  onLoad(){
    //获取所有的商品种类
    // wx.setStorageSync('', data)
    var meishi = []
    var yinpin = []
    var yiyao = []
    var shengxian = []
  //  this.setData({
  //    meishi
  //  })
    wx.cloud.callFunction({
      name:"getCata"
    }).then(res=>{
      console.log(res.result.data)
      this.setData({
        category:res.result.data
      })
    //获取所有的商品并这个装到数组准备渲染
     wx.cloud.callFunction({
       name:"getAllPro"
     }).then(res=>{
       console.log(res)
       this.setData({
         products:res.result.data
       })
       for(var x = 0; x < this.data.products.length; x++){
         var {caid} = this.data.products[x]
         console.log(caid)
         switch(caid){
          case 'meishi':
            meishi.push(this.data.products[x])
            break
          case 'yinpin':
            yinpin.push(this.data.products[x])
            break
          case 'yiyao':
            yiyao.push(this.data.products[x])
            break
          case 'shengxian':
            shengxian.push(this.data.products[x])
            break
         }
       }
       console.log(meishi)
       console.log("--------------")
       console.log(yinpin)
       this.setData({
         meishi,
         yinpin,
         yiyao,
         shengxian
       })
      //  console.log(this.data.category.length)
      //  for(var x = 0; x < this.data.category.length; x++){
      //    this.setData({
           
      //    })
      //    console.log(this.data.meishi)
      //  }
     })

     
    })
   
  }
  
})